const utils = require('../../../common/util/util')

Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        'xdata' : {
            type : Object ,
            value : {}
        }
    },
    observers: {
        /* 异步取值 */
        'xdata': function ( n ) {
            let that = this
            that.setData({
                eva : n.subjects
            })
        }
    },
    data: {
        oindex : 0 , // 当前题数初始化
        eva : [] ,
        endstate : false ,
        width: 0 ,
        warning : '' , // 测试评语
        maxwidth : '100' ,
        maxheight : '0' ,
    },
    methods: {
        /* 选择事件 */
        xSelect : function (e) {
            let that = this
            let width = 0
            let eva = that.data.eva // 当前题的列表
            let key = that.data.oindex // 当前的题数
            let index = e.currentTarget.dataset.index // 当前点击的选项
            
            for(var i in eva[key].options){
                delete eva[key].options[i].check
            }
            eva[key].options[index].check = true
            width = (key + 1) / eva.length.toFixed() * 100
            /* 最后一个问题处理 */
            if(key === eva.length - 1){
                that.setData({
                    eva : eva ,
                    width : width ,
                    endstate : true
                })
                return false
            }
            key = key + 1 > eva.length - 1 ? eva.length - 1 : key += 1
            that.setData({
                eva : eva ,
                width : width
            })
            setTimeout( () => {
                that.setData({
                    animation : 'animation' ,
                    oindex : key ,
                })
            } , 300)
        },

        aniamtion: function () {
            let that = this
            that.setData({
                animation: ''
            })
        },

        xPrev : function () {
            let that = this
            let key = that.data.oindex
            key = key - 1
            setTimeout( () => {
                that.setData({
                    animation : 'animations' ,
                    oindex : key ,
                })
            } , 300)
        },

        xSubmit : async function () {
            let that = this
            let eva = that.data.eva
            let data = that.properties.xdata
            let warning = data.results_assessment
            let xRes = 0
            for(var i in eva){
                let opt = eva[i].options
                for(var k in opt){
                    if(opt[k].check === true){
                        xRes += opt[k].score
                    }
                }
            }
            let end = {}
            for(var o in warning){
                if(warning[o].max_score >= xRes && xRes > warning[o].min_score){
                    end.results_assessment = warning[o]
                    end.score = xRes
                    that.setData({
                        warning : end
                    })
                }
            }
            data.subjects = eva
            wx.showLoading({
                title: '提交中',
            })
            await wx.cloud.callFunction({
                name: 'scale_manage',
                data: {
                    action: "add_scale_return_by_user",
                    dataObj:{
                        answer_results : data ,
                        test_results : end
                    }
                }
            }).then( async res => {
                console.log(end)
                await wx.showLoading({
                    title: '统计结果中',
                    success: () => {
                        let time = utils.formatTime_chinese(new Date())
                        that.setData({
                            maxwidth : '100' ,
                            maxheight : '100' ,
                            time : time ,
                            warning : end
                        })
                        wx.showToast({
                            title : '统计完成',
                            icon : 'none'
                        })
                    }
                })
            })
        },

        xBack : function () {
            wx.switchTab({
                url : '/pages/navigation/index/index',
            })
        }
    },
})